package com.atguigu.gulimall.seckill.interceptor;

import com.atguigu.common.constant.AuthConstant;
import com.atguigu.common.vo.MemberEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class LoginUserInterceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberEntityVo> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        MemberEntityVo attribute =(MemberEntityVo) request.getSession().getAttribute(AuthConstant.LOGIN_USER);
        if(new AntPathMatcher().match("/kill/**", requestURI)){
            if(attribute==null){
                log.debug("未登录");
                response.sendRedirect("http://auth.gulimall.com/login.html");
                return false;
            }else{
                log.debug("登录->>>用户ID：{}",attribute.getId());
                threadLocal.set(attribute);
                return true;
            }
        }
        return true;

    }

}
